package com.example.unimpdemo.util;

import android.content.res.Resources;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public enum FileUtil {
    ;

    public static void releaseFile(Resources res, int id, File file) throws IOException {
        File parentFile = file.getParentFile();
        if (!parentFile.exists()) {
            parentFile.mkdirs();
        }
        if (file.exists()) {
            file.delete();
        }
        file.createNewFile();
        InputStream inputStream = res.openRawResource(id);
        FileOutputStream outputStream = new FileOutputStream(file);
        BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
        byte[] buffer = new byte[1024];
        int len;
        while ((len = bufferedInputStream.read(buffer)) != -1) {
            outputStream.write(buffer, 0, len);
        }
        try {
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            bufferedInputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
